package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class PublishArticlePage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public static ForumListPage forumListPage;
    public static WebDriverWait wait;
    public PublishArticlePage() {
        super(url);
        forumListPage = new ForumListPage();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    /**
     * 发布帖子成功
     */
    public void PublishSuccess () {
        // 退出到之前最开始的页面
        driver.get("http://127.0.0.1:58080/index.html");
        // 再开始点击发布帖子
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        WebElement element = driver.findElement(By.cssSelector("#article_post_borad"));
        Select select = new Select(element);
        select.selectByIndex(5);
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("这是要发一个帖子"); // 标题
        WebElement content = driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll"));
        Actions actions = new Actions(driver);
        actions.click(content)
                .sendKeys("这是一篇新帖子")
                .build()
                .perform();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                // 使用 doubleValue() 进行比较
                return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
            }
        });
        driver.findElement(By.cssSelector("#article_post_submit")).click();
    }

    /**
     * 发布帖子失败
     * 标题和内容都为空
     */
    public void PublishFail () {
        // 标题和内容都为空
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                // 使用 doubleValue() 进行比较
                return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
            }
        });
        driver.findElement(By.cssSelector("#article_post_submit")).click();

        // 捕获错误
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("请输入帖子标题"); // 没有输入标题和内容
    }

    /**
     * 发布帖子失败
     * 标题不为空 | 内容为空
     */
    public void PublishFail1 () throws InterruptedException {
        // 退出到之前最开始的页面
        driver.get("http://127.0.0.1:58080/index.html");
        // 再开始点击发布帖子
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        // 标题不为空 | 内容为空
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("这是要发一个帖子"); // 标题
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        try {
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                            "return window.pageYOffset + window.innerHeight;");
                    Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                            "return document.body.scrollHeight;");
                    // 使用 doubleValue() 进行比较
                    return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
                }
            });
        } catch (Exception e) {
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                            "return window.pageYOffset + window.innerHeight;");
                    Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                            "return document.body.scrollHeight;");
                    // 使用 doubleValue() 进行比较
                    return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
                }
            });
        }
//        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#article_post_submit")).click();
        // 捕获错误
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("请输入帖子内容"); // 没有输入标题和内容
    }

    /**
     * 发布帖子失败
     * 标题为空 | 内容不为空
     */
    public void PublishFail2 () {
        // 退出到之前最开始的页面
        driver.get("http://127.0.0.1:58080/index.html");
        // 再开始点击发布帖子
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        // 标题为空 | 内容不为空
        driver.findElement(By.cssSelector("#article_post_title")).clear(); // 标题
        WebElement content = driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll"));
        Actions actions = new Actions(driver);
        actions.click(content)
                .sendKeys("这是一篇新帖子")
                .build()
                .perform();
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                // 使用 doubleValue() 进行比较
                return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
            }
        });
        driver.findElement(By.cssSelector("#article_post_submit")).click();
        driver.findElement(By.xpath("/html/body/div[4]/div"));
    }
}
